#! /usr/bin/env python2  
# encoding=utf-8
import sys
reload(sys) 
sys.setdefaultencoding('utf8')
import os
ScriptPath = os.path.split( os.path.realpath( sys.argv[0] ) )[0]
import xmlrpclib
import ConfigParser
import datetime

# 维保员工更新合同到期状态
config = ConfigParser.RawConfigParser()
config.read(ScriptPath+'/sys.cfg')
url = config.get('odoo','host')
uid = 1
pwd = config.get('odoo','password')
db = config.get('odoo','db')
models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))

def getToday():
    date = datetime.datetime.now().strftime('%Y-%m-%d')
    return date

def main():
    print u"开始更新员工合同过期"
    employees = models.execute_kw(db,uid,pwd,'tyibs.base.maintenance.company.employee','search_read',
        [[['contract_over_time','=',False],['contract_date','<',getToday()]]],{'fields':['name','contract_date','contract_over_time']}
    )
    ids = []
    for emp in employees:
        ids.append(emp['id'])
    models.execute_kw(db,uid,pwd,'tyibs.base.maintenance.company.employee','write',
        [ids,{'contract_over_time':True}]
    )


if __name__ == '__main__':
    main()